//
//  LoginViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/12.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "FeedbackViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *unameTF;
@property (nonatomic, strong) UITextField *pwdTF;

@end

@implementation LoginViewController

- (void) viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void) viewWillDisappear:(BOOL)animated {
    [_pwdTF resignFirstResponder];
    [_unameTF resignFirstResponder];
    [SVProgressHUD dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customizeBackItem];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    
    [self setStyle];
    
}

- (void) setStyle {
    self.navigationItem.title = @"登录";
    self.view.backgroundColor = GRAY_COLOR;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定"
                                                              style:UIBarButtonItemStyleDone
                                                             target:self
                                                             action:@selector(reqLoginAction)];
    self.navigationItem.rightBarButtonItem.tintColor = ORANGE_COLOR;
    
    UILabel *nameLB = [UILabel newAutoLayoutView];
    [self.view addSubview:nameLB];
    
    [nameLB autoPinToTopLayoutGuideOfViewController:self withInset:20];
    [nameLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [nameLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 50)];
    nameLB.backgroundColor = WHITE_COLOR;
    nameLB.textAlignment = NSTextAlignmentLeft;
    nameLB.text = TEXT_USERNAME;
    
    self.unameTF = [UITextField newAutoLayoutView];
    [self.view addSubview:_unameTF];
    
    [_unameTF autoAlignAxis:ALAxisHorizontal toSameAxisOfView:nameLB];
    [_unameTF autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:nameLB withOffset:80];
    [_unameTF autoSetDimensionsToSize:CGSizeMake(ScreenWidth - 100, 50)];
    _unameTF.placeholder = TEXT_INPUT_NICKNAME;
    _unameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _unameTF.delegate = self;
    
    UILabel *lineLB = [UILabel newAutoLayoutView];
    [self.view addSubview:lineLB];
    
    [lineLB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:nameLB];
    [lineLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:nameLB];
    [lineLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 0.5)];
    lineLB.backgroundColor = GRAY_COLOR;
    
    UILabel *pwdLB = [UILabel newAutoLayoutView];
    [self.view addSubview:pwdLB];
    
    [pwdLB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lineLB];
    [pwdLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:lineLB];
    [pwdLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 50)];
    pwdLB.backgroundColor = WHITE_COLOR;
    pwdLB.textAlignment = NSTextAlignmentLeft;
    pwdLB.text = TEXT_PWD;
    
    self.pwdTF = [UITextField newAutoLayoutView];
    [self.view addSubview:_pwdTF];
    
    [_pwdTF autoAlignAxis:ALAxisHorizontal toSameAxisOfView:pwdLB];
    [_pwdTF autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:pwdLB withOffset:80];
    [_pwdTF autoSetDimensionsToSize:CGSizeMake(ScreenWidth - 100, 50)];
    _pwdTF.placeholder = TEXT_INPUT_PWD;
    _pwdTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_pwdTF setSecureTextEntry:YES];
    _pwdTF.returnKeyType = UIReturnKeyDone;
    _pwdTF.delegate = self;
    
    UIButton *regBTN = [UIButton newAutoLayoutView];
    [self.view addSubview:regBTN];
    
    [regBTN autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:pwdLB withOffset:20];
    [regBTN autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:pwdLB];
    [regBTN autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 50)];
    regBTN.backgroundColor = WHITE_COLOR;
    [regBTN setTitle:TEXT_REG forState:UIControlStateNormal];
    [regBTN setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    regBTN.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [regBTN addTarget:self action:@selector(pushRegVC) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *rightArrow = [UIImageView newAutoLayoutView];
    [self.view addSubview:rightArrow];
    
    [rightArrow autoAlignAxis:ALAxisHorizontal toSameAxisOfView:regBTN];
    [rightArrow autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:regBTN withOffset:-20];
    [rightArrow autoSetDimensionsToSize:CGSizeMake(9, 15)];
    rightArrow.image = [UIImage imageNamed:@"rightArrow"];
    
    UILabel *line2LB = [UILabel newAutoLayoutView];
    [self.view addSubview:line2LB];
    
    [line2LB autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:regBTN];
    [line2LB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:regBTN];
    [line2LB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 0.5)];
    line2LB.backgroundColor = GRAY_COLOR;
    
    UIButton *forgotPwdBTN = [UIButton newAutoLayoutView];
    [self.view addSubview:forgotPwdBTN];
    
    [forgotPwdBTN autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:line2LB];
    [forgotPwdBTN autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:line2LB];
    [forgotPwdBTN autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 50)];
    forgotPwdBTN.backgroundColor = WHITE_COLOR;
    [forgotPwdBTN setTitle:TEXT_FORGOT_PWD forState:UIControlStateNormal];
    [forgotPwdBTN setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    forgotPwdBTN.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [forgotPwdBTN addTarget:self action:@selector(pushForgotVC) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *rightArrow2 = [UIImageView newAutoLayoutView];
    [self.view addSubview:rightArrow2];
    
    [rightArrow2 autoAlignAxis:ALAxisHorizontal toSameAxisOfView:forgotPwdBTN];
    [rightArrow2 autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:forgotPwdBTN withOffset:-20];
    [rightArrow2 autoSetDimensionsToSize:CGSizeMake(9, 15)];
    rightArrow2.image = [UIImage imageNamed:@"rightArrow"];
    
}

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    if (textField == _unameTF) {
        [_unameTF resignFirstResponder];
    }else{
        [_pwdTF resignFirstResponder];
    }
    return YES;
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_unameTF resignFirstResponder];
    [_pwdTF resignFirstResponder];
}

#pragma mark - 跳转注册页面
- (void) pushRegVC {
    RegisterViewController *regVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:regVC animated:YES];
}

#pragma mark - 跳转忘记密码
- (void) pushForgotVC {
    FeedbackViewController *feedBackVC = [[FeedbackViewController alloc] init];
    [self.navigationController pushViewController:feedBackVC animated:YES];
}

#pragma mark - 提交登录
- (void) reqLoginAction {
    
    if (_unameTF.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:TEXT_INPUT_NICKNAME maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    if (_pwdTF.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:TEXT_INPUT_PWD maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:_unameTF.text forKey:@"uname"];
    [parameters setObject:_pwdTF.text forKey:@"pwd"];
    
    [SVProgressHUD show];
    [[HttpManager instance] requestWithMethod:@"User/login"
                                   parameters:parameters
                                      success:^(NSDictionary *result) {
                                          NSDictionary *userInfoTmp = [result objectForKey:@"data"];
                                          NSString *ssidTmp = [userInfoTmp objectForKey:@"user_id"];
                                          NSString *alertText = [userInfoTmp objectForKey:@"info"];
                                          
                                          if (![ssidTmp isEqualToString:@""]) {
                                              [[TMCache sharedCache] setObject:ssidTmp forKey:@"SSID"];
                                          }
                                          
                                          [[TMCache sharedCache] setObject:userInfoTmp forKey:@"userInfo"];
                                          [[TMCache sharedCache] setObject:@"1" forKey:@"loginStatus"];
                                          
                                          [SVProgressHUD showSuccessWithStatus:alertText maskType:SVProgressHUDMaskTypeBlack];
                                          
                                          [self dismiss];
                                      }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          [SVProgressHUD showInfoWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
                                      }];
}

- (void) dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
